﻿namespace OverStore.Configuration.ComponentModel
{
    using OverStore.Runtime;

    /// <summary>
    /// Configuration that maintains single instance of <typeparamref name="TImplementation"/> class instantiated via default constructor.
    /// </summary>
    /// <typeparam name="TInterface">The type of the interface.</typeparam>
    /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
    public sealed class SingletonConfiguration<TInterface, TImplementation> : IComponentConfiguration<TInterface>
        where TInterface : IRuntimeComponent
        where TImplementation : class, TInterface, new()
    {
        /// <summary>
        /// Singleton instance of <see cref="IComponentConfiguration{TInterface}"/> type.
        /// </summary>
        public static readonly IComponentConfiguration<TInterface> Value = new SingletonConfiguration<TInterface, TImplementation>();

        /// <summary>
        /// Singleton instance of <typeparamref name="TImplementation"/> type.
        /// </summary>
        private static readonly TInterface Instance = new TImplementation();

        /// <summary>
        /// Initializes a new instance of the <see cref="SingletonConfiguration&lt;TInterface, TImplementation&gt;"/> class.
        /// </summary>
        private SingletonConfiguration() 
        { 
        }

        /// <summary>
        /// Creates the new instance of <typeparamref name="TComponent"/> type using set of services provided by <paramref name="context"/> object.
        /// </summary>
        /// <param name="context">The context provides access to services shared between components.</param>
        /// <returns>
        /// The new instance of <typeparamref name="TComponent"/> class.
        /// </returns>
        TInterface IComponentConfiguration<TInterface>.CreateComponent(OverStore.Runtime.OverStoreContext.IOverStoreContext context)
        {
            return Instance;
        }
    }
}
